package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.ProxyConfDao;
import com.baor.dao.ProxyLevelDao;
import com.baor.domain.ProxyLevelVo;
import com.baor.domain.ProxyVo;
import com.baor.domain.conf.ProxyConf;
import com.baor.service.ProxyConfService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProxyConfServiceImpl implements ProxyConfService {

    @Autowired
    private ProxyConfDao proxyConfDao;

    @Autowired
    private ProxyLevelDao proxyLevelDao;


    @Override
    public AjaxResult<?> isEnable(ProxyConf proxyConf) {
        LambdaQueryWrapper<ProxyConf> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProxyConf::getSiteId, proxyConf.getSiteId());
        int row = proxyConfDao.update(proxyConf, lqw);
        if (row > 0) {
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> levelList(Map<String, Object> params) {
        Page<ProxyLevelVo> page = getPage(params);
        LambdaQueryWrapper<ProxyLevelVo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProxyLevelVo::getDelFlag, "0")
                .eq(ProxyLevelVo::getSiteId, params.get("siteId"))
                .select(ProxyLevelVo.class, x -> !x.getColumn().equals("del_flag") && !x.getColumn().equals("site_id"));
        List<ProxyLevelVo> list = proxyLevelDao.selectPage(page, lqw).getRecords();
        list.forEach(x -> x.setSiteId(null));
        Map<String, Object> map = new HashMap<>();
        map.put("pageCount", page.getPages());
        map.put("list", list);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult<?> delete(ProxyLevelVo vo) {
        vo.setDelFlag("1");
        int row = proxyLevelDao.updateById(vo);
        if (row > 0) {
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> insert(ProxyLevelVo vo) {
        LambdaQueryWrapper<ProxyLevelVo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProxyLevelVo::getLevelName, vo.getLevelName())
                .eq(ProxyLevelVo::getSiteId, vo.getSiteId());
        if(proxyLevelDao.selectOne(lqw)!= null){
            return AjaxResult.error("代理等级名称已存在");
        }
        int row = proxyLevelDao.insert(vo);
        if (row > 0) {
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> update(ProxyLevelVo proxyLevelVo) {
        int row = proxyLevelDao.updateById(proxyLevelVo);
        if (row > 0) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    public int insert(int siteId){
        ProxyConf proxyConf = new ProxyConf();
        proxyConf.setSiteId(String.valueOf(siteId));
        int row =proxyConfDao.insert(proxyConf);
        if (row > 0){
            return 1;
        }else{
            return 0;
        }
    }

    private Page<ProxyLevelVo> getPage(Map<String,Object> map){
        if ( map.get("pageIndex") == null || "".equals(map.get("pageIndex")) )
            map.put("pageIndex",1);
        if ( map.get("pageSize") == null || "".equals(map.get("pageSize")) )
            map.put("pageSize",10);
        Long pageIndex = Long.valueOf(map.get("pageIndex").toString());
        Long pageSize = Long.valueOf(map.get("pageSize").toString());
        Page<ProxyLevelVo> page = new Page<>(pageIndex,pageSize);
        return page;
    }



}
